package com.fish.yssb.ui.page.activity.other;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.fish.yssb.R;
import com.fish.yssb.api.retData;
import com.fish.yssb.api.setApi;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.data.model.HisFileData;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.db.RealmDb;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.db.bean.LogInfoTable;
import com.fish.yssb.db.bean.UserInfoTable;
import com.fish.yssb.ui.page.activity.base.BaseActivity;
import com.fish.yssb.ui.page.activity.his.hisDetailYCActivity;
import com.fish.yssb.ui.page.adapter.LogParaAdapter;
import com.fish.yssb.ui.page.adapter.UserManageParaAdapter;
import com.fish.yssb.ui.page.dialog.PublicNoticeDialog;
import com.fish.yssb.ui.page.dialog.getDataByTimeDialog;
import com.jaeger.library.StatusBarUtil;
import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


/**
 * A login screen that offers login via email/password.
 */
public class UserManageActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = UserManageActivity.class.getSimpleName();

    LinearLayout tv_back;
    private TimePickerView pvTime;
    private TextView tv_updata;
    private int timeType = 1;
    private RecyclerView recyclerView;
    private UserManageParaAdapter adapter;

    private ArrayList<UserInfoTable> arrayList;

    public static void start(Context context) {
        Intent intent = new Intent(context, UserManageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setDarkMode(this);
        setContentView(R.layout.activity_usermanage_list);
        initview();
        EventBus.getDefault().register(this);

        int ret = setApi.getInstance(this).r_const_para();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    //初始化视图
    @SuppressLint("WrongViewCast")
    private void initview() {
        //登录层、下拉层、其它登录方式层
        recyclerView = (RecyclerView) findViewById(R.id.recylerview);
        recyclerView.setHasFixedSize(true);


        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter = new UserManageParaAdapter());

        adapter.activity = UserManageActivity.this;
        adapter.replaceAll(getData());
        adapter.notifyDataSetChanged();
        tv_back = findViewById(R.id.tv_back);

        tv_back.setOnClickListener(this);
        tv_updata = findViewById(R.id.tv_updata);
        tv_updata.setOnClickListener(this);
    }

    @Subscribe
    public void onEvent(final retData result) {

    }

    public void refeshdata()
    {
        getData();
        adapter.replaceAll(getData());
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onResume() {
        super.onResume();
        refeshdata();
    }
    private PublicNoticeDialog noticeDialog;

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.tv_back:
                finish();
                break;

            case R.id.tv_updata:
                final LoginUser user = BaseApp.getInstance().user;
                if(!"管理员".equalsIgnoreCase(user.getUserType())) {
                    noticeDialog = new PublicNoticeDialog(UserManageActivity.this, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String sendStatus = "";
                            if (view.getId() == R.id.btn_chancel_notice) {//暂存

                                noticeDialog.dismiss();
                            } else {//生成

                                noticeDialog.dismiss();

                            }
                            noticeDialog.dismiss();

                        }
                    }, 4, "", "非管理员用户无权限查进行用户管理操作", "取消", "确定");
                    noticeDialog.show();
                }else {
                    AddUserActivity.start(this);
                }

                break;
            default:
                break;
        }


    }

    public ArrayList<UserInfoTable> getData()
    {
        arrayList = (ArrayList) RealmDb
                .getInstance()
                .queryRealmObjects(UserInfoTable.class);

        return arrayList;
    }

}

